package com.example.learn.service;

import com.example.learn.entity.Account;
import com.example.learn.entity.DebtStatistics;

import com.example.learn.entity.User;
import com.example.learn.mapper.AccountMapper;
import com.example.learn.mapper.DebtStatisticsMapper;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;

@Service
public class DebtstatisticalService {
    @Resource
    AccountMapper accountMapper;
    @Resource
    DebtStatisticsMapper debtStatisticsMapper;

    public List<DebtStatistics> getDebtstatistical(Boolean isfamily, User user) {
        Set<String> names=new HashSet();
        if(isfamily){
            //先找出家庭人员
            List<Account>  accounts= accountMapper.getFamilyName(user.getName());

            for (Account account:accounts
            ) {
                names.add(account.getName());
                names.add(account.getAccount());
            }
        }else{
            names.add(user.getName());
        }
        List<DebtStatistics> incomeList=    debtStatisticsMapper.getByName(names);
        return incomeList;
    }

    public String addDebtstatistical(DebtStatistics debtstatistical, User user) {
        if(StringUtils.isEmpty(debtstatistical.getId())){
            debtstatistical.setId(UUID.randomUUID().toString());
            debtstatistical.setName(user.getName());
            debtstatistical.setDisable(true);
            int i=      debtStatisticsMapper.insert(debtstatistical);
        }else {
            debtstatistical.setName(user.getName());
            debtstatistical.setDisable(true);

            debtStatisticsMapper.updateByPrimaryKey(debtstatistical);
        }

        return "ok";
    }

    public String deleteDebtstatistical(String id) {
        int i=  debtStatisticsMapper.deleteByPrimaryKey(id);
        return  "ok";
    }
}
